var db = require('../../core/database.js'),
	fs = require('fs'),
	cronJob = require('cron').CronJob,
	gcalendar = require('./gcalendar.js'),
	util = require('../../core/module.js'),
	enocean = require('../../core/enocean.js');

var config = new util.ConfigModule('morning'); // configuration file

var jobs = {'calendar': [], 'coffee': []}; // cron jobs for each alarm


function onCalendarCron(idInstance)
{
	config.get(idInstance, function(err, conf) {
		if (err) {
			console.log(err);
		} else {
			gcalendar.sayCalendar(conf['access_token']);
			console.log('Calendar cron #'+idInstance+' triggered.');
		}
	});
}

function onCoffeeCron(idInstance)
{
	enocean.toggleSwitch(true);
	setTimeout(function()
	{
		enocean.toggleSwitch(false);
		console.log('Stopping coffee machine.');
	}, 60000); // wait 60 seconds
	console.log('Coffee cron #'+idInstance+' triggered. Starting machine.');
}

function createCalendarCron(idInstance, hours, minutes)
{
	if(jobs['calendar'][idInstance] != null)
		jobs['calendar'][idInstance].stop(); // stop the job if it is already running

	jobs['calendar'][idInstance] = new cronJob(
		'00 '+minutes+' '+hours+' * * *', // Runs every day
		function(){onCalendarCron(idInstance);},
	 	null, 
	  	true /* Start the job right now */
	);

	console.log('Set calendar cron #'+idInstance+' to '+hours+':'+minutes);
}


function createCoffeeCron(idInstance, hours, minutes)
{
	if(jobs['coffee'][idInstance] != null)
		jobs['coffee'][idInstance].stop(); // stop the job if it is already running

	jobs['coffee'][idInstance] = new cronJob(
		'00 '+minutes+' '+hours+' * * *', // Runs every day
		function(){onCoffeeCron(idInstance);},
	 	null, 
	  	true /* Start the job right now */
	);

	console.log('Set coffee cron #'+idInstance+' to '+hours+':'+minutes);
}

function setAlarm(idInstance)
{
	config.get(idInstance, function(err, conf) // read config for the module
	{
		// if gcalendar params exist :
		if(conf['gcalendar_hour'] != undefined && conf['gcalendar_min'] != undefined)
			createCalendarCron(idInstance, conf['gcalendar_hour'], conf['gcalendar_min']);
		else
			console.log('Could not read gcalendar config for the module #'+idInstance);

		// if coffee params exist :
		if(conf['coffee_hour'] != undefined && conf['coffee_min'] != undefined)
			createCoffeeCron(idInstance, conf['coffee_hour'], conf['coffee_min']);
		else
			console.log('Could not read coffee config for the module #'+idInstance);
	});
}


function initAlarm(idInstance)
{
	setAlarm(idInstance); // set the alarm with id "idInstance"
	fs.watchFile(__dirname + '/conf.json', function(){setAlarm(idInstance);}); // watch for changes in the configuration file
	console.log('Watch file :'+__dirname + '/conf.json');
}


function initModule (idModule)
{
	initAlarm(idModule); // init the alarm
	console.log('-*-*-*- '+ idModule + '-*-*-*-');

}

function createModule (moduleName, params, callback)
{
	db.createModule(moduleName, function (idModule)
	{
	
		
		config.set(idModule, {
		'gcalendar_hour': params.gcalendar_hour, 
		'gcalendar_min': params.gcalendar_min,
		'coffee_hour': params.coffee_hour,
		'coffee_min': params.coffee_min,
		'access_token': params.token}, function(error) {
			initModule(idModule);
			callback(idModule);
		});

		console.log('create module morning with access token : '+params.token);
		
	});
}

function remove (params) {
	//TODO
}

var requirements = ["outlet"]; // nothing

module.exports.init = initModule;
module.exports.create = createModule;
module.exports.remove = remove;
module.exports.requirements = requirements;
